package BinaryTree;
/**
 * 完全二叉树的节点个数
 */
import BinaryTree.init.TreeNode;

public class lc222 {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        int rightlen = 0;
        int leftlen = 0;
        TreeNode left = root.left;
        TreeNode right = root.right;
        while (left != null) {
            left = left.left;
            leftlen++;
        }
        while (right != null) {
            right = right.right;
            rightlen++;
        }
        if (rightlen == leftlen) {
            return (2 << leftlen) - 1;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;

    }
}
